Passed
Push — master ( 587d17...41e48a )
by EMP
01:27
created

main.js ➔ getCountryFlag   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
1
"use strict";
2
3
sodium.ready.then(function() {
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
5
const ae = new AllEars(function(ok) {
1 ignored issue
show
Bug introduced by
The variable AllEars seems to be never declared. If this is a global, consider adding a /** global: AllEars */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
	if (ok) {
7
		document.getElementById("txt_skey").style.background = "#466";
8
		document.getElementById("txt_skey").maxLength = "64";
9
	} else {
10
		console.log("Failed to load All-Ears");
11
	}
12
});
13
14
function TabState(cur, max, btnDele, btnUpdt) {
15
	this.cur = cur;
16
	this.max = max;
17
	this.btnDele = btnDele;
18
	this.btnUpdt = btnUpdt;
19
}
20
21
const tabs = [
22
	new TabState(0, 0, false, true), // Inbox
23
	new TabState(0, 0, false, true), // Outbx
24
	new TabState(0, 2, true, false), // Write
25
	new TabState(0, 0, false, false), // Notes
26
	new TabState(0, 0, false, true) // Admin
27
];
28
29
let tab = 0;
30
const TAB_INBOX = 0;
31
const TAB_OUTBX = 1;
0 ignored issues
show
Unused Code introduced by
The constant TAB_OUTBX seems to be never used. Consider removing it.
Loading history...
32
const TAB_WRITE = 2;
33
const TAB_NOTES = 3;
0 ignored issues
show
Unused Code introduced by
The constant TAB_NOTES seems to be never used. Consider removing it.
Loading history...
34
const TAB_ADMIN = 4;
0 ignored issues
show
Unused Code introduced by
The constant TAB_ADMIN seems to be never used. Consider removing it.
Loading history...
35
36
// Helper functions
37
function getCountryName(countryCode) {
38
	const opts = document.getElementById("gatekeeper_country");
39
40
	for (let i = 0; i < opts.length; i++) {
41
		if (opts[i].value === countryCode) {
42
			return opts[i].textContent;
43
		}
44
	}
45
46
	return "Unknown countrycode: " + countryCode;
47
}
48
49
function getCountryFlag(countryCode) {
50
	return sodium.to_string(new Uint8Array([
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
51
		240, 159, 135, 166 + countryCode.codePointAt(0) - 65,
52
		240, 159, 135, 166 + countryCode.codePointAt(1) - 65
53
	]));
54
}
55
56
function getMsgId(num) {
57
	let i;
58
	if (ae.GetExtMsgHeaders(num).toLowerCase().slice(0, 11) === "message-id:") {
59
		i = 0;
60
	} else {
61
		i = ae.GetExtMsgHeaders(num).toLowerCase().indexOf("\nmessage-id:");
62
		if (i < 1) return "ERR";
63
		i++;
64
	}
65
66
	const x = ae.GetExtMsgHeaders(num).slice(i + 11).trim();
67
	if (x[0] !== "<") return "ERR2";
68
	return x.slice(1, x.indexOf(">"));
69
}
70
71
function displayMsg(isInt, num) {
72
	document.getElementById("midright").scroll(0, 0);
73
74
	const ts = isInt? ae.GetIntMsgTime(num) : ae.GetExtMsgTime(num);
75
76
	document.getElementById("btn_reply").disabled = false;
77
	document.getElementById("btn_reply").onclick = function() {
78
		document.getElementById("write_recv").value = isInt? ae.GetIntMsgFrom(num) : ae.GetExtMsgFrom(num);
79
		document.getElementById("write_subj").value = "Re: " + (isInt ? ae.GetIntMsgTitle(num) : ae.GetExtMsgTitle(num));
80
		document.getElementById("write_rply").textContent = (isInt? "" : getMsgId(num));
81
		document.getElementById("btn_write").click();
82
		document.getElementById("div_write_1").hidden = false;
83
		document.getElementById("div_write_2").hidden = true;
84
		document.getElementById("write_body").focus();
85
		for (const opt of document.getElementById("write_from").options) {
86
			if (opt.value === (isInt ? ae.GetIntMsgTo(num) : ae.GetExtMsgTo(num))) {
87
				opt.selected = true;
88
			}
89
		}
90
	};
91
92
	document.getElementById("msg").hidden = false;
93
	document.getElementById("msg").getElementsByTagName("h1")[0].textContent = isInt ? ae.GetIntMsgTitle(num) : ae.GetExtMsgTitle(num);
94
	document.getElementById("msg").getElementsByTagName("pre")[0].textContent = isInt ? ae.GetIntMsgBody(num) : ae.GetExtMsgBody(num);
95
96
	document.getElementById("readmsg_to").textContent = isInt ? ae.GetIntMsgTo(num) : ae.GetExtMsgTo(num);
97
	document.getElementById("readmsg_date").children[0].textContent = new Date(ts * 1000).toISOString().slice(0, 19).replace("T", " ");
98
99
	if (!isInt) {
100
		document.getElementById("readmsg_ip").hidden = false;
101
		document.getElementById("readmsg_country").hidden = false;
102
		document.getElementById("readmsg_tls").hidden = false;
103
		document.getElementById("readmsg_greet").hidden = false;
104
		document.getElementById("readmsg_timing").hidden = false;
105
		document.getElementById("readmsg_envfrom").hidden = false;
106
107
		const cc = ae.GetExtMsgCountry(num);
108
109
		document.getElementById("readmsg_ip").children[0].textContent = ae.GetExtMsgIp(num);
110
		document.getElementById("readmsg_country").textContent = getCountryFlag(cc) + " " + getCountryName(cc);
111
		document.getElementById("readmsg_tls").children[0].textContent = ae.GetExtMsgTLS(num);
112
		document.getElementById("readmsg_greet").children[0].textContent = ae.GetExtMsgGreet(num);
113
		document.getElementById("readmsg_envfrom").textContent = ae.GetExtMsgFrom(num);
114
115
		let flagText = "";
116
		if (!ae.GetExtMsgFlagVPad(num)) flagText += "<abbr title=\"Invalid padding\">PAD</abbr> ";
117
		if (!ae.GetExtMsgFlagVSig(num)) flagText += "<abbr title=\"Invalid signature\">SIG</abbr> ";
118
		if (!ae.GetExtMsgFlagPExt(num)) flagText += "<abbr title=\"The sender did not use the Extended (ESMTP) protocol\">SMTP</abbr> ";
119
		if (!ae.GetExtMsgFlagQuit(num)) flagText += "<abbr title=\"The sender did not issue the required QUIT command\">QUIT</abbr> ";
120
		if (ae.GetExtMsgFlagRare(num)) flagText += "<abbr title=\"The sender issued unusual command(s)\">RARE</abbr> ";
121
		if (ae.GetExtMsgFlagFail(num)) flagText += "<abbr title=\"The sender issued invalid command(s)\">FAIL</abbr> ";
122
		if (ae.GetExtMsgFlagPErr(num)) flagText += "<abbr title=\"The sender violated the protocol\">PROT</abbr> ";
123
		document.getElementById("readmsg_flags").children[0].innerHTML = flagText.trim();
124
	} else {
125
		document.getElementById("readmsg_ip").hidden = true;
126
		document.getElementById("readmsg_country").hidden = true;
127
		document.getElementById("readmsg_tls").hidden = true;
128
		document.getElementById("readmsg_greet").hidden = true;
129
		document.getElementById("readmsg_timing").hidden = true;
130
		document.getElementById("readmsg_envfrom").hidden = true;
131
132
		let symbol = "<span title=\"Invalid level\">&#x26a0;</span>";
133
		if (ae.GetIntMsgFrom(num) === "system") {if (ae.GetIntMsgLevel(num) === 3) symbol = "<span title=\"System\">&#x1f162;</span>";} // S (System)
134
		else if (ae.GetIntMsgLevel(num) === 0) symbol = "<span title=\"Level 0 User\">&#x1f10c;</span>"; // 0
135
		else if (ae.GetIntMsgLevel(num) === 1) symbol = "<span title=\"Level 1 User\">&#x278a;</span>"; // 1
136
		else if (ae.GetIntMsgLevel(num) === 2) symbol = "<span title=\"Level 2 User\">&#x278b;</span>"; // 2
137
		else if (ae.GetIntMsgLevel(num) === 3) symbol = "<span title=\"Administrator\">&#x1f150;</span>"; // A (Admin)
138
		document.getElementById("readmsg_from").innerHTML = symbol + " " + ae.GetIntMsgFrom(num);
139
140
		let flagText = "";
141
		if (!ae.GetIntMsgFlagVPad(num)) flagText += "<abbr title=\"Invalid padding\">PAD</abbr> ";
142
		if (!ae.GetIntMsgFlagVSig(num)) flagText += "<abbr title=\"Invalid signature\">SIG</abbr> ";
143
		document.getElementById("readmsg_flags").children[0].innerHTML = flagText.trim();
144
	}
145
}
146
147
// Interface
148
function addMsg(isInt, i) {
149
	const inbox = document.getElementById("tbl_inbox");
150
	const sent = document.getElementById("tbl_outbx");
151
152
	const isSent = false; //TODO
153
	const table = isSent ? sent : inbox;
154
155
	const row = table.insertRow(-1);
156
	const cellTime = row.insertCell(-1);
157
	const cellSubj = row.insertCell(-1);
158
	const cellSnd1 = row.insertCell(-1);
159
	const cellSnd2 = row.insertCell(-1);
160
161
	const ts = isInt? ae.GetIntMsgTime(i) : ae.GetExtMsgTime(i);
162
	cellTime.setAttribute("data-ts", ts);
163
	cellTime.textContent = new Date(ts * 1000).toISOString().slice(0, 10);
164
165
	cellSubj.textContent = isInt? ae.GetIntMsgTitle(i) : ae.GetExtMsgTitle(i);
166
167
	if (isInt) {
168
		cellSnd1.textContent = ae.GetIntMsgFrom(i);
169
		cellSnd1.className = (ae.GetIntMsgFrom(i).length === 16) ? "mono" : "";
170
	} else {
171
		const from1 = ae.GetExtMsgFrom(i);
172
		const from2 = from1.substring(from1.indexOf("@") + 1);
173
		const cc = ae.GetExtMsgCountry(i);
174
175
		cellSnd1.textContent = from1.substring(0, from1.indexOf("@"));
176
177
		const flag = document.createElement("abbr");
178
		flag.textContent = getCountryFlag(cc);
179
		flag.title = getCountryName(cc);
180
		cellSnd2.appendChild(flag);
181
182
		const fromText = document.createElement("span");
183
		fromText.textContent = " " + from2;
184
		cellSnd2.appendChild(fromText);
185
	}
186
187
//	divDel.innerHTML = "<input class=\"delMsg\" type=\"checkbox\" data-id=\"" + ae.GetIntMsgIdHex(i) + "\">";
188
189
	row.onclick = function() {
190
		displayMsg(isInt, i);
191
	};
192
/*
193
	cellDel.children[0].onchange = function() {
194
		if (!divDel.children[0].checked) {
195
			const checkboxes = elmt.getElementsByTagName("input");
196
			let checked = false;
197
198
			for (let j = 0; j < checkboxes.length; j++) {
199
				if (checkboxes[j].checked) {
200
					checked = true;
201
					break;
202
				}
203
			}
204
205
			if (!checked) {
206
				document.getElementById(isSent ? "btn_sentdel" : "btn_msgdel").hidden = true;
207
				return;
208
			}
209
		}
210
211
		document.getElementById(isSent? "btn_sentdel" : "btn_msgdel").hidden = false;
212
	};
213
*/
214
}
215
216
function getRowsPerPage() {
217
	const tbl = document.getElementById("tbl_inbox");
218
	tbl.innerHTML = "";
219
	const row = tbl.insertRow(-1);
220
	const cell = row.insertCell(-1);
221
	cell.textContent = "0";
222
223
	const rowsPerPage = Math.floor(getComputedStyle(document.getElementById("div_inbox")).height.replace("px", "") / getComputedStyle(document.querySelector("#tbl_inbox > tbody > tr:first-child")).height.replace("px", "")) - 1; // -1 allows space for 'load more'
224
	tbl.innerHTML = "";
225
	return rowsPerPage;
226
}
227
228
function addMessages() {
229
	const rowsPerPage = getRowsPerPage();
230
	let skipMsgs = rowsPerPage * tabs[TAB_INBOX].cur;
231
232
	const maxExt = ae.GetExtMsgCount();
233
	const maxInt = ae.GetIntMsgCount();
234
235
	tabs[TAB_INBOX].max = Math.floor((maxExt + maxInt) / rowsPerPage);
236
237
	let numExt = 0;
238
	let numInt = 0;
239
	let numAdd = 0;
240
241
	while (numAdd < rowsPerPage) {
242
		const tsInt = (numInt < maxInt) ? ae.GetIntMsgTime(numInt) : -1;
243
		const tsExt = (numExt < maxExt) ? ae.GetExtMsgTime(numExt) : -1;
244
		if (tsInt === -1 && tsExt === -1) break;
245
246
		if (tsInt !== -1 && (tsExt === -1 || tsInt > tsExt)) {
247
			if (skipMsgs > 0) skipMsgs--; else {addMsg(true, numInt); numAdd++;}
248
			numInt++;
249
		} else if (tsExt !== -1) {
250
			if (skipMsgs > 0) skipMsgs--; else {addMsg(false, numExt); numAdd++;}
251
			numExt++;
252
		}
253
	}
254
255
	if (ae.GetReadyMsgKilos() < ae.GetTotalMsgKilos()) {
256
		const inbox = document.getElementById("tbl_inbox");
257
		const row = inbox.insertRow(-1);
258
		const cell = row.insertCell(-1);
259
		cell.textContent = "Load more (" + (ae.GetTotalMsgKilos() - ae.GetReadyMsgKilos()) + " KiB left)";
260
261
		row.onclick = function() {
262
			this.onclick = "";
263
264
			ae.Message_Browse(false, function(successBrowse) {
265
				document.getElementById("tbl_inbox").style.opacity = 1;
266
267
				if (successBrowse) {
268
					addMessages();
269
					if (tabs[tab].cur < tabs[tab].max) document.getElementById("btn_rght").disabled = false;
270
				}
271
			});
272
		};
273
	}
274
}
275
276
function updateAddressCounts() {
277
	document.getElementById("limit_normal").textContent = (ae.GetAddressCountNormal() + "/" + ae.GetAddressLimitNormal(ae.GetUserLevel())).padStart(ae.GetAddressLimitNormal(ae.GetUserLevel()) > 9 ? 5 : 1);
278
	document.getElementById("limit_shield").textContent = (ae.GetAddressCountShield() + "/" + ae.GetAddressLimitShield(ae.GetUserLevel())).padStart(ae.GetAddressLimitShield(ae.GetUserLevel()) > 9 ? 5 : 1);
279
	document.getElementById("limit_total").textContent = ((ae.GetAddressCountNormal() + ae.GetAddressCountShield()) + "/" + ae.GetAddrPerUser()).padStart(5);
280
}
281
282
function reloadAccount() {
283
	// Contacts
284
	for (let i = 0; i < ae.GetContactCount(); i++) {
285
		addContact(
286
			ae.GetContactMail(i),
287
			ae.GetContactName(i),
288
			ae.GetContactNote(i)
289
		);
290
	}
291
292
	// Addresses
293
	for (let i = 0; i < ae.GetAddressCount(); i++) {
294
		addAddress(i);
295
	}
296
297
	document.getElementById("table_addrs").getElementsByTagName("caption")[0].textContent = "Level " + ae.GetUserLevel() + " User";
298
	updateAddressCounts();
299
}
300
301 View Code Duplication
function deleteAddress(addr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
302
	let btns = document.getElementById("tbl_addrs").getElementsByTagName("button");
303
	for (let i = 0; i < btns.length; i++) btns[i].disabled = true;
304
305
	let addressToDelete = -1;
306
307
	for (let i = 0; i < ae.GetAddressCount(); i++) {
308
		if (addr === ae.GetAddress(i)) {
309
			addressToDelete = i;
310
			break;
311
		}
312
	}
313
314
	if (addressToDelete === -1) return;
315
316
	ae.Address_Delete(addressToDelete, function(success) {
317
		if (success) {
318
			document.getElementById("tbl_addrs").deleteRow(addressToDelete);
319
			document.getElementById("write_from").remove(addressToDelete);
320
			updateAddressCounts();
321
322
			if (ae.GetAddressCountNormal() < ae.GetAddressLimitNormal(ae.GetUserLevel())) document.getElementById("btn_address_create_normal").disabled = false;
323
			if (ae.GetAddressCountShield() < ae.GetAddressLimitShield(ae.GetUserLevel())) document.getElementById("btn_address_create_shield").disabled = false;
324
325
			ae.Private_Update(function(success2) {
326
				if (!success2) console.log("Failed to update the Private field");
327
328
				btns = document.getElementById("tbl_addrs").getElementsByTagName("button");
329
				for (let i = 0; i < btns.length; i++) btns[i].disabled = false;
330
			});
331
		} else {
332
			console.log("Failed to delete address");
333
334
			btns = document.getElementById("tbl_addrs").getElementsByTagName("button");
335
			for (let i = 0; i < btns.length; i++) btns[i].disabled = false;
336
		}
337
	});
338
}
339
340 View Code Duplication
function shieldMix(addr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
341
	let newAddr = "";
342
343
	for (let i = 0; i < 16; i++) {
344
		switch (addr.charAt(i)) {
345
			case '1':
346
				newAddr += "1iIlL".charAt(Math.floor(Math.random() * 5));
347
				break;
348
			case '0':
349
				newAddr += "0oO".charAt(Math.floor(Math.random() * 3));
350
				break;
351
			case 'w':
352
				newAddr += "VvWw".charAt(Math.floor(Math.random() * 4));
353
				break;
354
			default:
355
				newAddr += (Math.random() > 0.5) ? addr.charAt(i) : addr.charAt(i).toUpperCase();
356
		}
357
	}
358
359
	return newAddr;
360
}
361
362
function addAddress(num) {
363
	const addrTable = document.getElementById("tbl_addrs");
364
	const row = addrTable.insertRow(-1);
365
	const cellAddr = row.insertCell(-1);
366
	const cellChk1 = row.insertCell(-1);
367
	const cellChk2 = row.insertCell(-1);
368
	const cellChk3 = row.insertCell(-1);
369
	const cellBtnD = row.insertCell(-1);
370
371
	cellAddr.textContent = ae.GetAddress(num);
372
	cellAddr.onclick = function() {
373
		if (cellAddr.textContent.length === 16)
374
			navigator.clipboard.writeText(shieldMix(cellAddr.textContent) + "@" + ae.GetDomainEml());
1 ignored issue
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
375
		else
376
			navigator.clipboard.writeText(cellAddr.textContent + "@" + ae.GetDomainEml());
377
	};
378
379
	cellChk1.innerHTML = ae.GetAddressAccExt(num) ? "<input type=\"checkbox\" checked=\"checked\">" : "<input type=\"checkbox\">";
380
	cellChk2.innerHTML = ae.GetAddressAccInt(num) ? "<input type=\"checkbox\" checked=\"checked\">" : "<input type=\"checkbox\">";
381
	cellChk3.innerHTML = ae.GetAddressUse_Gk(num) ? "<input type=\"checkbox\" checked=\"checked\">" : "<input type=\"checkbox\">";
382
383
	cellBtnD.innerHTML = "<button type=\"button\">X</button>";
384
	cellBtnD.onclick = function() {deleteAddress(cellAddr.textContent);};
385
386
	const opt = document.createElement("option");
387
	opt.value = cellAddr.textContent;
388
	opt.textContent = cellAddr.textContent + "@" + ae.GetDomainEml();
389
	document.getElementById("write_from").appendChild(opt);
390
}
391
392
document.getElementById("btn_dele").onclick = function() {
393
	this.blur();
394
395
	if (tab === TAB_WRITE) {
396
		tabs[tab].cur = 0;
397
		updateTab();
398
399
		document.getElementById("write_recv").value = "";
400
		document.getElementById("write_subj").value = "";
401
		document.getElementById("write_body").value = "";
402
403
		document.getElementById("write_recv").focus();
404
	}
405
};
406
407
document.getElementById("btn_updt").onclick = function() {
408
	const btn = this;
409
	btn.disabled = true;
410
	btn.blur();
411
412
	if (tab === TAB_INBOX) {
413
		document.getElementById("tbl_inbox").style.opacity = 0.5;
414
415
		ae.Message_Browse(true, function(successBrowse) {
416
			document.getElementById("tbl_inbox").style.opacity = 1;
417
418
			if (successBrowse) {
419
				addMessages();
420
				btn.disabled = false;
421
			} else {
422
				console.log("Failed to refresh");
423
				btn.disabled = false;
424
			}
425
		});
426
	}
427
};
428
429
function addContact(mail, name, note) {
430
	const tbl = document.getElementById("tbl_ctact");
431
	const row = tbl.insertRow(-1);
432
	const cellMail = row.insertCell(-1);
433
	const cellName = row.insertCell(-1);
434
	const cellNote = row.insertCell(-1);
435
	const cellBtnD = row.insertCell(-1);
436
437
	cellMail.textContent = mail;
438
	cellName.textContent = name;
439
	cellNote.textContent = note;
440
	cellBtnD.innerHTML = "<button type=\"button\">X</button>";
441
442
	cellMail.contentEditable = true;
443
	cellName.contentEditable = true;
444
	cellNote.contentEditable = true;
445
446
	cellBtnD.onclick = function() {row.remove();};
447
}
448
449
document.getElementById("btn_newcontact").onclick = function() {
450
	addContact("", "", "");
451
};
452
453
document.getElementById("btn_savecontacts").onclick = function() {
454
	while (ae.GetContactCount() > 0) {
455
		ae.DeleteContact(0);
456
	}
457
458
	for (const row of document.getElementById("tbl_ctact").rows) {
459
		ae.AddContact(row.cells[0].textContent, row.cells[1].textContent, row.cells[2].textContent);
460
	}
461
462
	const btn = this;
463
	btn.disabled = true;
464
465
	ae.Private_Update(function(success) {
466
		btn.disabled = false;
467
468
		if (!success) {
469
			console.log("Failed contacts update");
470
		}
471
	});
472
};
473
474
function updateTab() {
475
	switch (tab) {
476
		case TAB_INBOX:
477
			addMessages();
478
		break;
479
480
		case TAB_WRITE:
481
			switch (tabs[tab].cur) {
482
				case 0: // Write
483
					document.getElementById("div_write_1").hidden = false;
484
					document.getElementById("div_write_2").hidden = true;
485
					document.getElementById("write_body").focus();
486
				break;
487
488
				case 1: // Verify
489
					ae.Address_Lookup(document.getElementById("write_recv").value, function(pk) {
490
						if (pk) {
491
							document.getElementById("div_write_1").hidden = true;
492
							document.getElementById("div_write_2").hidden = false;
493
494
							document.getElementById("write2_from").textContent = document.getElementById("write_from").value + "@" + ae.GetDomainEml();
495
							document.getElementById("write2_recv").textContent = document.getElementById("write_recv").value;
496
							document.getElementById("write2_pkey").textContent = sodium.to_hex(pk);
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
497
498
							document.getElementById("write2_subj").textContent = document.getElementById("write_subj").value;
499
							document.getElementById("write2_rply").textContent = document.getElementById("write_rply").textContent;
500
							document.getElementById("write2_body").textContent = document.getElementById("write_body").value;
501
						} else {
502
							console.log("Failed lookup");
503
						}
504
					});
505
				break;
506
507
				case 2: // Send
508
					ae.Message_Create(
509
						document.getElementById("write_subj").value,
510
						document.getElementById("write_body").value,
511
						document.getElementById("write_from").value,
512
						document.getElementById("write_recv").value,
513
						document.getElementById("write_rply").textContent,
514
						(document.getElementById("write2_recv").textContent.indexOf("@") > 0) ? null : sodium.from_hex(document.getElementById("write2_pkey").textContent),
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
515
						function(success) {
516
							if (success) {
517
								console.log("Sent ok");
518
							} else {
519
								console.log("Failed sending");
520
							}
521
						}
522
					);
523
				break;
524
			}
525
		break;
526
	}
527
528
	document.getElementById("btn_left").disabled = (tabs[tab].cur === 0);
529
	document.getElementById("btn_rght").disabled = (tabs[tab].cur === tabs[tab].max);
530
}
531
532
document.getElementById("btn_left").onclick = function() {
533
	tabs[tab].cur--;
534
	if (tabs[tab].cur === 0) this.disabled = true;
535
	if (tabs[tab].cur < tabs[tab].max) document.getElementById("btn_rght").disabled = false;
536
	updateTab();
537
	this.blur();
538
};
539
540
document.getElementById("btn_rght").onclick = function() {
541
	tabs[tab].cur++;
542
	if (tabs[tab].cur === tabs[tab].max) this.disabled = true;
543
	document.getElementById("btn_left").disabled = false;
544
	updateTab();
545
	this.blur();
546
};
547
548
const buttons = document.querySelector("#main1 > .top").getElementsByTagName("button");
549
for (let i = 0; i < buttons.length; i++) {
550
	buttons[i].onclick = function() {
551
		tab = i;
552
553
		for (let j = 0; j < buttons.length; j++) {
554
			document.querySelector("#main1 > .mid").children[j].hidden = (tab !== j);
555
			buttons[j].disabled = (tab === j);
556
		}
557
558
		document.getElementById("btn_left").disabled = (tabs[tab].cur === 0);
0 ignored issues
show
Bug introduced by
The variable tab is changed as part of the for loop for example by i on line 551. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
559
		document.getElementById("btn_rght").disabled = (tabs[tab].cur === tabs[tab].max);
560
		document.getElementById("btn_dele").disabled = !tabs[tab].btnDele;
561
		document.getElementById("btn_updt").disabled = !tabs[tab].btnUpdt;
562
	};
563
}
564
565
function addressCreate(addr) {
566
	const btnN = document.getElementById("btn_address_create_normal");
567
	const btnS = document.getElementById("btn_address_create_shield");
568
	btnN.disabled = true;
569
	btnS.disabled = true;
570
571
	ae.Address_Create(addr, function(success1) {
572
		if (success1) {
573
			ae.Private_Update(function(success2) {
574
				addAddress(ae.GetAddressCount() - 1);
575
				if (addr !== "SHIELD") document.getElementById("txt_address_create_normal").value = "";
576
				updateAddressCounts();
577
578
				if (!success2) console.log("Failed to update the Private field");
579
580
				if (ae.GetAddressCountNormal() < ae.GetAddressLimitNormal(ae.GetUserLevel())) btnN.disabled = false;
581
				if (ae.GetAddressCountShield() < ae.GetAddressLimitShield(ae.GetUserLevel())) btnS.disabled = false;
582
			});
583
		} else {
584
			console.log("Failed to add address");
585
586
			if (ae.GetAddressCountNormal() < ae.GetAddressLimitNormal(ae.GetUserLevel())) btnN.disabled = false;
587
			if (ae.GetAddressCountShield() < ae.GetAddressLimitShield(ae.GetUserLevel())) btnS.disabled = false;
588
		}
589
	});
590
}
591
592
document.getElementById("btn_address_create_normal").onclick = function() {
593
	if (ae.GetAddressCountNormal() >= ae.GetAddressLimitNormal(ae.GetUserLevel())) return;
594
595
	const txtNewAddr = document.getElementById("txt_address_create_normal");
596
	if (!txtNewAddr.reportValidity()) return;
597
598
	addressCreate(txtNewAddr.value);
599
};
600
601
document.getElementById("btn_address_create_shield").onclick = function() {
602
	if (ae.GetAddressCountShield() >= ae.GetAddressLimitShield(ae.GetUserLevel())) return;
603
604
	addressCreate("SHIELD");
605
};
606
607
document.getElementById("txt_skey").onkeyup = function(event) {
608
	if (event.key === "Enter") {
609
		event.preventDefault();
610
		document.getElementById("btn_enter").click();
611
	}
612
};
613
614
document.getElementById("btn_enter").onclick = function() {
615
	const txtSkey = document.getElementById("txt_skey");
616
	if (!txtSkey.reportValidity()) return;
617
618
	const btn = this;
619
	btn.disabled = true;
620
	document.getElementById("txt_skey").style.background = "#233";
621
622
	ae.SetKeys(txtSkey.value, function(successSetKeys) {
623
		if (successSetKeys) {
624
			ae.Account_Browse(0, function(successBrowse) {
625
				if (successBrowse) {
626
					txtSkey.value = "";
627
628
					reloadAccount();
629
					document.getElementById("div_begin").hidden = true;
630
					document.getElementById("div_main").style.display = "grid";
631
632
					document.getElementById("btn_updt").click();
633
				} else {
634
					console.log("Failed to enter");
635
					btn.disabled = false;
636
					document.getElementById("txt_skey").style.background = "#466";
637
					txtSkey.focus();
638
				}
639
			});
640
		} else {
641
			console.log("Invalid format for key");
642
			btn.disabled = false;
643
			document.getElementById("txt_skey").style.background = "#466";
644
			txtSkey.focus();
645
		}
646
	});
647
};
648
649
});
650